package com.jvm123.meilisearch.config;

import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author yawn http://jvm123.com
 * 2020/1/15 15:53
 */
@Configuration
@EnableConfigurationProperties(MeillisearchProperties.class)
public class MeilisearchAutoConfiguration {

    @Resource
    MeillisearchProperties properties;

    @Bean
    @ConditionalOnMissingBean(Client.class)
    Client client() {
        return new Client(config());
    }

    @Bean
    @ConditionalOnMissingBean(Config.class)
    Config config() {
        return new Config(properties.getHostUrl(), properties.getApiKey());
    }

}
